package com.ukao.cashregister.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.listener.OnItemClickListener;
import com.ukao.cashregister.widget.StateImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ukaodev on 2017/6/22.
 */

public class HomeAdapter    extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {


    private final Resources mResources;
    private   Context  mContext;
    private  List<String>   list;
    public     HomeAdapter(Context  mContext, List<String>   list){
        this.mContext=mContext;
        this.list=list;
        mResources=mContext.getResources();
    }

    private OnItemClickListener mOnClickListener;

    public  void  setOnClickListener(OnItemClickListener mOnClickListener){
        this.mOnClickListener=mOnClickListener;

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_home_fragment, parent,
                false));



        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.itemname.setText(list.get(position));
        Drawable normal;
        Drawable pressed;
        pressed=mResources.getDrawable(Constant.mHomeSelectIcons[position]);
        normal=mResources.getDrawable(Constant.mUnHomeSelectIcons[position]);

        holder.itemicon.setStateBackground(pressed,normal,normal);
        holder.itemView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (mOnClickListener!=null)
                    mOnClickListener.onSureItemClick(list.get(position), position );

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.item_home_fragment_itemicon)
        StateImageView itemicon;
        @BindView(R.id.item_home_fragment_itemname)
        TextView itemname;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
